﻿using Common.Device.DeviceDb;
using Common.Device.Interfaces;
using Common.Device.Models;
using Common.Device.Views;
using Common.Device.Views.Devices;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace Common.Device.ViewModels
{
    public class OrderEditViewModel:Common.View.Base.BindableBase
    {
        private UseDevice device;
        /// <summary>
        /// 当前使用仪器
        /// </summary>
        public UseDevice Device
        {
            get { return device; }
            set { SetProperty(ref device, value); }
        }
        private List<string> deviceNames = new List<string>(DeviceService.useDevices.Keys);
        /// <summary>
        /// 仪器标识集合
        /// </summary>
        public List<string> DeviceNames
        {
            get { return deviceNames; }
            set { SetProperty(ref deviceNames, value); }
        }
        private string deviceName;
        /// <summary>
        /// 仪器标识
        /// </summary>
        public string DeviceName
        {
            get { return deviceName; }
            set { SetProperty(ref deviceName, value); }
        }
        private string name;
        /// <summary>
        /// 测试项名称
        /// </summary>
        public string Name
        {
            get { return name; }
            set { SetProperty(ref name, value); }
        }
        private int testTimes = 0;
        /// <summary>
        /// 重测次数
        /// </summary>
        public int TestTimes
        {
            get { return testTimes; }
            set { SetProperty(ref testTimes, value); }
        }
        private bool ngStop = true;
        /// <summary>
        /// 失败停止
        /// </summary>
        public bool NgStop
        {
            get { return ngStop; }
            set { SetProperty(ref ngStop, value); }
        }
        private ContentControl deviceView;
        /// <summary>
        /// 当前选择的仪器界面
        /// </summary>
        public ContentControl DeviceView
        {
            get { return deviceView; }
            set { SetProperty(ref deviceView, value); }
        }
        private ObservableCollection<Models.DeviceOrder> orders = new ObservableCollection<Models.DeviceOrder>();
        /// <summary>
        /// 测试指令集合
        /// </summary>
        public ObservableCollection<Models.DeviceOrder> Orders
        {
            get { return orders; }
            set { SetProperty(ref orders, value); }
        }
        public ICommand DelCommand { get => new DelegateCommand<Models.DeviceOrder>(Del); }
        public void Del(Models.DeviceOrder order)
        {
            //删除测试指令
            var result = View.Controls.Dialogs.TipView.Show($"确认删除指令[{order.index}]?", new Dictionary<string, Brush> { { "删除", View.Base.Colors.PrimaryColor }, { "取消", View.Base.Colors.DangerColor } });
            if (result == "删除")
            {
                try
                {
                    //移除
                    orders.Remove(order);
                    //重新计算序号
                    for (int i = 0; i < orders.Count; i++)
                    {
                        orders[i].index = i + 1;
                        orders[i].no = Encoding.ASCII.GetString(new[] { (byte)(Encoding.ASCII.GetBytes("a")[0] + i) });
                    }
                    //刷新界面
                    Orders = new System.Collections.ObjectModel.ObservableCollection<DeviceOrder>(orders);
                }
                catch (Exception ex)
                {
                    HandyControl.Controls.MessageBox.Error("删除指令失败" + ex.Message); return;
                }
            }
        }
        /// <summary>
        /// 加载编辑界面
        /// </summary>
        /// <param name="order">测试指令</param>
        public void SetDeviceView(Models.Order order = null)
        {
            //解析测试指令到编辑界面
            if (Device == null)
            {
                DeviceView = new FuncView(order);
                return;
            }
            var idevice = DeviceService.DeviceFactory(Device, null);
            switch (Device.type)
            {
                case Enums.DeviceEnum.DeviceTypeEnum.绝缘耐压:
                    DeviceView = new JueYuanNaiYaView(Device.model, order);
                    (DeviceView as JueYuanNaiYaView).Device = idevice as IJueYuanNaiYa; break;
                case Enums.DeviceEnum.DeviceTypeEnum.温度采集:
                    DeviceView = new CaiJiMoKuaiView(Device.model,order);
                    (DeviceView as CaiJiMoKuaiView).Device = idevice as ICaiJiMoKuai; break;
                case Enums.DeviceEnum.DeviceTypeEnum.电压采集:
                    DeviceView = new DianYaCaiJiView(Device.model, order);
                    (DeviceView as DianYaCaiJiView).Device = idevice as IDianYaCaiJi; break;
                case Enums.DeviceEnum.DeviceTypeEnum.电阻仪:
                    DeviceView = new CeLiangYiView(Device.model, order);
                    (DeviceView as CeLiangYiView).Device = idevice as IDianZuYi; break;
                case Enums.DeviceEnum.DeviceTypeEnum.数字电桥:
                    DeviceView = new CeLiangYiView(Device.model, order);
                    (DeviceView as CeLiangYiView).Device = idevice as IShuZiDianQiao; break;
                case Enums.DeviceEnum.DeviceTypeEnum.万用表:
                    DeviceView = new CeLiangYiView(Device.model, order);
                    (DeviceView as CeLiangYiView).Device = idevice as IWanYongBiao; break;
                case Enums.DeviceEnum.DeviceTypeEnum.交流电源:
                case Enums.DeviceEnum.DeviceTypeEnum.直流电源:
                    DeviceView = new DianYuanView(Device.model, order);
                    (DeviceView as DianYuanView).Device = idevice as IDianYuan; break;
                case Enums.DeviceEnum.DeviceTypeEnum.电子负载:
                    DeviceView = new DianZiFuZaiView(Device.model, order);
                    (DeviceView as DianZiFuZaiView).Device = idevice as IDianZiFuZai; break;
                case Enums.DeviceEnum.DeviceTypeEnum.示波器:
                    DeviceView = new ShiBoQiView(Device.model,order);
                    (DeviceView as ShiBoQiView).Device = idevice as IShiBoQi; break;
                case Enums.DeviceEnum.DeviceTypeEnum.FCT测试板:
                    DeviceView = new FctView(Device,order);
                    (DeviceView as FctView).Device = idevice as IFct; break;
                case Enums.DeviceEnum.DeviceTypeEnum.FCT测试板3:
                    DeviceView = new FctView(Device, order);
                    (DeviceView as FctView).Device = idevice as IFct3; break;
                case Enums.DeviceEnum.DeviceTypeEnum.通信测试:
                    DeviceView = new TongXinView(order);
                    (DeviceView as TongXinView).Device = idevice as ITongXin; break;
                case Enums.DeviceEnum.DeviceTypeEnum.车身域:
                    DeviceView = new GeChaoJiCheView(Device.model, order);
                    (DeviceView as GeChaoJiCheView).Device = idevice as IGeChaoJi; break;
                case Enums.DeviceEnum.DeviceTypeEnum.智控域:
                    DeviceView = new ZhiKongView(Device.model, order);
                    (DeviceView as ZhiKongView).Device = idevice as IZhiKong; break;
                default:
                    break;
            }
        }
        public ICommand DelALLCommand { get => new DelegateCommand(DelAll); }
        public void DelAll()
        {
            var result = View.Controls.Dialogs.TipView.Show($"确认删除所有指令?", new Dictionary<string, Brush> { { "删除", View.Base.Colors.PrimaryColor }, { "取消", View.Base.Colors.DangerColor } });
            if (result == "删除")
            {
                try
                {
                    orders.Clear();
                    Orders = new System.Collections.ObjectModel.ObservableCollection<DeviceOrder>(orders);
                }
                catch (Exception ex)
                {
                    HandyControl.Controls.MessageBox.Error("删除指令失败" + ex.Message); return;
                }
            }
        }
    }
}
